
package com.kugou.game.sdk.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 描述:自定义圆形的ImageView,可设置边缘宽度和颜色。边缘宽度和颜色属性在attrs.xml有定义
 * 
 * @author liux
 * @since 2013-10-11 下午05:26:48
 */

public class RoundImageView extends ImageView {
    private int mBorderThickness = 5;

    private Context mContext;

    private int mBorderColor = 0x50ffffff;

    public RoundImageView(Context context) {
        super(context);
        mContext = context;
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        // setCustomAttributes(attrs);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        // setCustomAttributes(attrs);
    }

    // private void setCustomAttributes(AttributeSet attrs) {
    // try {
    // TypedArray a = mContext.obtainStyledAttributes(attrs,
    // Rx.styleable.roundImageView);
    // mBorderThickness = a.getDimensionPixelSize(
    // Rx.styleable.roundImageView_border_thickness, 0);
    // mBorderColor = a.getColor(Rx.styleable.roundImageView_border_color,
    // mBorderColor);
    // a.recycle();
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    // }

    
    
    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }
        this.measure(0, 0);
        if (drawable.getClass() == NinePatchDrawable.class)
            return;
        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        if (b == null || b.isRecycled()) {
            return;
        }
        Bitmap bitmap = b.copy(Config.ARGB_8888, true);

        int w = getWidth(), h = getHeight();
        int radius = (w < h ? w : h) / 2 - mBorderThickness;
        Bitmap roundBitmap = getCroppedBitmap(bitmap, radius);

        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        paint.setColor(mBorderColor);
        canvas.drawCircle(w / 2, h / 2, radius + mBorderThickness, paint);
        canvas.drawBitmap(roundBitmap, w / 2 - radius, h / 2 - radius, null);
    }

    public int getmBorderColor() {
        return mBorderColor;
    }

    public void setmBorderColor(int mBorderColor) {
        this.mBorderColor = mBorderColor;
    }

    /**
     * 设置裁剪的Bitmap
     * 
     * @param bmp 需要裁剪的Bitmap
     * @param radius 裁剪成圆形的半径
     * @return
     */
    public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
        Bitmap scaledSrcBmp;
        int diameter = radius * 2;
        if (bmp.getWidth() != diameter || bmp.getHeight() != diameter) {
            scaledSrcBmp = Bitmap.createScaledBitmap(bmp, diameter, diameter, false);
        } else {
            scaledSrcBmp = bmp;
        }
        Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight(),
                Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight());

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.parseColor("#ffffff"));
        canvas.drawCircle(scaledSrcBmp.getWidth() / 2, scaledSrcBmp.getHeight() / 2,
                scaledSrcBmp.getWidth() / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);
        return output;
    }

}
